package vn.gq.app4vn.doctruyen;

import vn.gq.cd.adutils.BannerHelper;
import vn.gq.cd.adutils.BannerHelper.BannerType;
import vn.gq.cd.model.bean.CategoryItem;
import vn.gq.cd.view.CategoryFragment;
import vn.gq.cd.view.ChapterFragment;
import vn.gq.cd.widget.BaseFragment;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.NetworkUtils;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.actionbarsherlock.view.Menu;


public class MainActivity extends BaseActivity implements IClientListener {
	
	public static final String TAG = "MainActivity";
	
	private Handler mHandler = new Handler();
	private BaseFragment fragment;
	private BannerHelper mBannerHelper;
	
	private Runnable showDialogRunnable = new Runnable() {
		
		@Override
		public void run() {
			new BookmarkManager(MainActivity.this).showDialogConfirm();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_layout);
		LinearLayout adLayout = (LinearLayout) findViewById(R.id.adLayout);
		BannerType adType = (Configuration.TYPE_AD_ADMOB) ? BannerType.BANNER : BannerType.O_BANNER;
		mBannerHelper = new BannerHelper(this, adLayout, adType);
		if (!getString(R.string.author).equals("")) {
			getSupportActionBar().setSubtitle(getString(R.string.author));
		}
		if (Configuration.MULTI_STORIES_APP) {
			fragment = CategoryFragment.newInstance("data");
		} else {
			fragment = ChapterFragment.newInstance(new CategoryItem("asset://gzip:data", ""), new BookmarkManager(this).getBookmarkChapterPos());
		}
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		if (getSupportFragmentManager().findFragmentByTag(fragment.getFragmentTag()) != null) {
			ft.remove(fragment);
		}
		ft.add(fragment.getContainerId(), fragment, fragment.getFragmentTag()).commit();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (!NetworkUtils.newInstance(this).hasConnection()) {
			Toast.makeText(getApplicationContext(), "Không có kết nối mạng, vui lòng kiểm tra lại kết nối!", Toast.LENGTH_SHORT).show();
		}
		if (mBannerHelper != null) mBannerHelper.loadAd();
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mBannerHelper != null) mBannerHelper.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mBannerHelper != null) mBannerHelper.onDestroy();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.none, menu);
		return true;
	}

//	@Override
//	public void onBackPressed() {
//		mHandler.removeCallbacks(showDialogRunnable);
////		super.onBackPressed();
//		
//		if (mFragment != null) {
//			mFragment.onBackPressed();
//		} else {
//			super.onBackPressed();
//		}
//		
//	}

	@Override
	public void onDone(int code, Object result) {
		mHandler.postDelayed(showDialogRunnable, 100);
	}
}
